package com.carsa.blackberry.domain.dto;

import java.util.Enumeration;
import java.util.Vector;

import com.carsa.blackberry.MainKeys;
import com.carsa.blackberry.util.XMLHandler;
import com.carsa.blackberry.util.persistence.Persistable;

/**
 * @author Nahuel Barrios.
 * @since 12/08/2010.
 * @version 1.0
 */
public class QueueDTO implements Persistable {

    public static final String XML_ELEMENT_QUEUE          = "queue";
    public static final String XML_ELEMENT_MESSAGES       = "messages";
    public static final String XML_ELEMENT_IS_ANY_MESSAGE = "isAnyMessage";

    private String             _xmlVersionTag             = "";
    private boolean            _hasMessages;

    private Vector             _messages                  = new Vector();

    public QueueDTO() {
	super();
    }
    
    public String toString() {
	return this.toXmlString();
    }

    /**
     * @param xmlSource
     *            El {@link String} en formato XML del cual se deben recuperar los atributos para setear el objeto this.
     */
    public QueueDTO(String xmlSource) {
	super();
	this.initializeFromXML(xmlSource);
    }

    /**
     * @param xmlSource
     *            El {@link String} en formato XML del cual se deben recuperar los atributos para setear el objeto this.
     */
    public int initializeFromXML(String xmlSource) {
	XMLHandler xml = new XMLHandler(xmlSource);
	_xmlVersionTag = xml.getXmlVersionTag();
	_hasMessages = xml.getElementValue(XML_ELEMENT_IS_ANY_MESSAGE).equals(String.valueOf(true));

	xml = new XMLHandler(xml.getElementValue(XML_ELEMENT_MESSAGES));
	int elementsCount = xml.getElementCount();

	int idx = 0;
	for (idx = 0; idx < elementsCount; idx++) {
	    _messages.addElement(new QueueMessageDTO(xml.getFirstElementValueOverIteration()));
	}

	return idx;
    }

    /**
     * Adds aMessage parameter to this {@link QueueDTO} object.
     * 
     * @param aMessage
     *            The {@link QueueMessageDTO} to add.
     * @return <code>true</code> when the message was added to the messages list. Or <code>false</code> when aMessage is
     *         <code>null</code> and it was not added.
     */
    public boolean addMessage(QueueMessageDTO aMessage) {
	if (aMessage != null) {
	    this._messages.addElement(aMessage);
	    this._hasMessages = true;
	    return true;
	}

	return false;
    }

    /**
     * @return the messages. It's a {@link Vector} containing a {@link QueueMessageDTO} object for each message from the
     *         queue.
     */
    public Vector getMessages() {
	return _messages;
    }

    /**
     * @param messages
     *            the messages to set. It has to be a {@link Vector} containing a {@link QueueMessageDTO} object for
     *            each message for the queue.
     */
    public void setMessages(Vector messages) {
	this._messages = messages;

	_hasMessages = !this._messages.isEmpty();
    }

    /**
     * @return <code>true</code> when this {@link QueueDTO} object has messages to persist. Otherwise <code>false</code>
     *         .
     */
    public boolean hasMessages() {
	return this._hasMessages;
    }

    /**
     * @return the xmlVersionTag
     */
    public String getXmlVersionTag() {
	return _xmlVersionTag;
    }

    /**
     * @param xmlVersionTag
     *            the xmlVersionTag to set
     */
    public void setXmlVersionTag(String xmlVersionTag) {
	_xmlVersionTag = xmlVersionTag;
    }

    public String toXmlString() {
	XMLHandler xml = new XMLHandler(XML_ELEMENT_QUEUE, null, null);
	xml.setXmlVersionTag(MainKeys.XML_VERSION_TAG);

	xml.addElementAtRoot(XML_ELEMENT_IS_ANY_MESSAGE, null, String.valueOf(this.hasMessages()));

	XMLHandler xmlMessages = new XMLHandler(XML_ELEMENT_MESSAGES, null, null);
	Enumeration elements = _messages.elements();
	while (elements.hasMoreElements()) {
	    QueueMessageDTO aMessage = (QueueMessageDTO) elements.nextElement();

	    xmlMessages.addElementAtRoot(aMessage.toXmlString());
	}
	xml.addElementAtRoot(xmlMessages.toXMLString());

	return xml.toString();
    }

    public String getPersistPath() {
	return MainKeys.FILE_PATH_PERSISTENCE_QUEUE;
    }

    public String getStringToPersist() {
	return this.toXmlString();
    }

}
